package com.qf.listener;

import com.qf.constans.SystemParameter;
import com.qf.feign.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Map;
import java.util.Set;

/**
 * 在zuul启动是将redis中的服务信息存放到本地缓存
 */

@Component
public class MyContextLoadlistener implements ServletContextListener {
    Logger logger = LoggerFactory.getLogger(MyContextLoadlistener.class);
    @Resource
    private RedisService redisService;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        Set<String> keys = redisService.keys(SystemParameter.REDISAPIPRE + "*");

        SystemParameter.apiinfoMap.clear();

        for (String key : keys) {

            Map<Object, Object> map = redisService.hgetAll(key);
            SystemParameter.apiinfoMap.put(key,map);
        }
        logger.error("初始化api信息,得到的结果是:{}", SystemParameter.apiinfoMap);

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
